<template>
  <div class="login_container">
      <div class="login_box">
          <!-- 头像区域 -->
          <div class="avatar_box">
              <img src="../assets/logo.png" alt="">
          </div>
          <!-- 登录表单区域 -->
          <el-form ref="loginFormRef"  :rules="rules"  :model="loginForm" class="login_form">
            <!-- 用户名 -->
            <el-form-item label="" prop="username">
                <el-input type="text" maxlength=12  prefix-icon="el-icon-s-custom" v-model="loginForm.username"></el-input>
            </el-form-item>
            <!-- 密码 -->
            <el-form-item label="" prop="password" >
                <el-input  maxlength=12 show-password prefix-icon="el-icon-lock" v-model="loginForm.password"></el-input>
            </el-form-item>
            <!-- 按钮区域 -->
            <el-form-item class="btns" ref="btns">
                <el-button type="primary" @click="login">登录</el-button>
                <el-button type="info" @click="resetLoginForm">重置</el-button>
            </el-form-item>
          </el-form>
      </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
      return {
          //这是登录表单的数据对象
          loginForm:{
              username:'admin',
              password:'123456'
          },

          rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
          ],
          password:[
              { required: true, message: '请输入密码', trigger: 'blur' },
              { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
          ]
        }
      }
  },
  methods:{
      //表单验证
      login() {
        this.$refs.loginFormRef.validate(async (valid) => {
          if (valid) {
            // alert('submit!');
            const {data:res}=await this.$http.post('login',this.loginForm)
            if(res.meta.status!==200)return this.$message.error('登录失败')
            this.$message.success("登录成功")
            //存储token,浏览器关闭后自动删除
            sessionStorage.setItem('token',res.data.token)
            //通过编程式导航跳转到后台主页,路由地址是/home
            this.$router.push('/home')
          } else {
            console.log('error submit!!');
            return false;
          }
        })
      },
      //重置按钮
      resetLoginForm(){
          console.log(this)
          this.$refs.loginFormRef.resetFields();
      }
  }
};
</script>

<style lang='less' scoped>
.login_container {
  background-color: blue;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  margin: auto;
  //第二种居中方式
  // left:50%;
  // top:50%;
  // transform:translate(-50%,-50%);
}
.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  box-shadow: 0 0 10px #ddd;
  padding: 10px;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
 
}
 .login_form{
      position:absolute;
      bottom:0;
      width:100%;
      padding:0 8%;
      box-sizing:border-box;
  }
  .btns{
      display:flex;
      justify-content:center;
      align-items: center;
  }
</style>